package com.atguigu.jxc.controller;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.PurchaseListDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/purchaseListGoods")
public class PurchaseListGoodsController {

    @Autowired
    private PurchaseListDao purchaseListDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private GoodsDao goodsDao;
    /**
     * 进货单保存
     * http://localhost:8080/purchaseListGoods/save?purchaseNumber=JH1605768306735
     */
    @PostMapping("/save")
    @ResponseBody
    public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr){
        System.out.println("=====================================");
        if(purchaseList == null || purchaseListGoodsStr == null){
            return null;
        }
        // 使用谷歌Gson将JSON字符串数组转换成具体的集合
        Gson gson = new Gson();

        List<PurchaseListGoods> purchaseListGoodsList = gson.fromJson(purchaseListGoodsStr,new TypeToken<List<PurchaseListGoods>>(){}.getType());

        // 设置当前操作用户
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());

        purchaseList.setUserId(currentUser.getUserId());

        purchaseListDao.insertPurchaseList(purchaseList);

        for(PurchaseListGoods purchaseListGoods : purchaseListGoodsList){
            purchaseListGoods.setPurchaseListId(purchaseList.getPurchaseListId());

            purchaseListDao.insertPurchaseListGoods(purchaseListGoods);

            // 修改商品库存，状态
            Goods goods = goodsDao.findByGoodsId(purchaseListGoods.getGoodsId());

            goods.setInventoryQuantity(goods.getInventoryQuantity()+purchaseListGoods.getGoodsNum());

            goods.setState(1);

            goodsDao.updateGoods(goods);

        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**todo:模糊查询还有问题
     * 进货单列表展示（可条件查询：单据号模糊、供应商、是否付款和日期区间）
     * http://localhost:8080/purchaseListGoods/list
     */
    @PostMapping("/list")
    @ResponseBody
    public Map<String,Object> list(String purchaseNumber, Integer supplierId, Integer state, String sTime,String eTime){
        //返回我的进货单
        List<String> list = purchaseListDao.findPurchaseList(purchaseNumber,supplierId,state,sTime,eTime);
        System.out.println("==========================="+list);
        if (list == null){
            return null;
        }
        //根据进货单id,查询出所有的进货单上的商品
        Map<String,Object> map = new HashMap<>();
        map.put("rows", list);
        return map;
    }

    /**查询进货单商品信息
     * http://localhost:8080/purchaseListGoods/goodsList
     */
    @PostMapping("/goodsList")
    @ResponseBody
    public Map<String,Object>  findGoodsList(Integer purchaseListId){
        List list = purchaseListDao.findGoodsList(purchaseListId);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        return map;
    }

    /**进货单删除
     * http://localhost:8080/purchaseListGoods/delete
     */
    @PostMapping("/delete")
    @ResponseBody
    public ServiceVO deletePurchaseList(Integer purchaseListId){
        purchaseListDao.deletePurchaseList(purchaseListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**供应商统计
     *支付结算（修改进货单付款状态）
     * http://localhost:8080/purchaseListGoods/updateState
     */
    @PostMapping("/updateState")
    @ResponseBody
    public ServiceVO updateState(Integer purchaseListId){
        purchaseListDao.updateState(purchaseListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**商品采购统计
     *进货统计（可根据 商品类别、商品编码或名称 条件查询）
     * http://localhost:8080/purchaseListGoods/count
     */
    @PostMapping("/count")
    @ResponseBody
    public String count(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        List<String> countList = purchaseListDao.count(sTime,eTime,goodsTypeId,codeOrName);

        return countList.toString();
    }


}

